package hn.cch.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import hn.cch.aspect.param.TestParam;
import hn.cch.constant.Letter;
import hn.cch.server.response.LetterRespMsg;
import hn.cch.server.result.TestResult;
import hn.cch.server.test.TestClient;
import hn.cch.util.BytesUtil;
import hn.cch.util.Des3Util;
import hn.cch.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Client {
    private static Logger logger = LoggerFactory.getLogger(Client.class);

    public static void main(String[] args) throws InterruptedException {


        logger.info("******************************");
        String host = "127.0.0.1";
        // String host = "192.168.10.145";
        String port = "8808";
        String url = "http://" + host +":" + port + "/http/api/test?type=main&user=XXXX&hash=XXXXXXXXXXXXXXXXX";
        logger.info("url : " + url);
        logger.info("******************************");

        TestParam testParam = new TestParam();
        testParam.setFunction("testFunction");
        TestParam.TestContent testContent = testParam.new TestContent();
        testContent.setKey("testKey");
        testContent.setValue("testValue");
        logger.info("param : " + JSON.toJSONString(testContent));
        testParam.setContent(testContent);
        byte[] post = JsonUtil.toBytes(testParam);

        logger.info("******************************");

        TestClient testClient = new TestClient();
        testClient.connectURL(url);
        logger.debug("3des key :" + BytesUtil.toHex(Des3Util.key));
        byte[] data = Des3Util.crypt(Des3Util.CRYPT.EN, post, Des3Util.key);
        byte[] temp =testClient.sendPOST(data);
        byte[] crypt = Des3Util.crypt(Des3Util.CRYPT.DE, temp, Des3Util.key);


        logger.info("******************************");
        LetterRespMsg letterRespMsg = (LetterRespMsg) JsonUtil.toObject(crypt, LetterRespMsg.class);
        if (letterRespMsg.getCode().equals(Letter.SUCCESS)) {
            JSONObject jsonObject = (JSONObject) letterRespMsg.getContent();

            TestResult testResult = new TestResult(jsonObject.getString("test0"), jsonObject.getString("test1"), jsonObject.getString("test2"), jsonObject.getString("test3"), jsonObject.getString("test4"), jsonObject.getString("test5"), jsonObject.getString("test6"), jsonObject.getString("test7"), jsonObject.getString("test8"), jsonObject.getString("test9"));
            logger.info("result : " + JSON.toJSONString(testResult));
        }


        logger.info("******************************");




    }
}
